package com.das.mechanic_alone.mvp.view.alone.utils;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.das.mechanic_alone.R;
import com.das.mechanic_alone.mvp.view.alone.fragment.X3AloneHotCommentFragment;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.zhpan.indicator.IndicatorView;
import com.zhpan.indicator.enums.IndicatorSlideMode;
import com.zhpan.indicator.enums.IndicatorStyle;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class AloneHotCommentHelper {

    private AloneHotCommentHelper(){}
    private static class SingleInstance{
        private static final AloneHotCommentHelper instance = new AloneHotCommentHelper();
    }
    public static AloneHotCommentHelper getInstance(){
        return  SingleInstance.instance;
    }

    ViewPager vp_main;
    RelativeLayout rl_indicator;
    FragmentActivity activity;
    IndicatorView id_view;

    List<String> commentList = new ArrayList<>();

    public void setVp(FragmentActivity activity,ViewPager vp_main,IndicatorView id_view,RelativeLayout rl_indicator,OnHotClick onHotClick){
        this.vp_main = vp_main;
        this.activity = activity;
        this.id_view = id_view;
        this.onHotClick = onHotClick;
        this.rl_indicator = rl_indicator;
    }

    public void changeVpData(List<String> commentList,View contentView) {

        if (vp_main == null || activity == null){
            return;
        }

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.clear();

        ArrayList<ArrayList<String>> allDatas = new ArrayList<>();

        List<String> tmpList = new ArrayList<>();
        if (!X3StringUtils.isListEmpty(commentList)){
            for (String  str: commentList){
                if (!X3StringUtils.isEmpty(str)){
                    tmpList.add(str);
                }
            }
        }

        if (tmpList.size()>0 && contentView!=null){
            contentView.setVisibility(View.VISIBLE);
        }

        if (!X3StringUtils.isListEmpty(tmpList)){

            for (int i = 0; i < tmpList.size(); i++){

                if (i % 2 == 1){
                    ArrayList<String> datas = new ArrayList<String>();
                    datas.add(tmpList.get(i-1));
                    datas.add(tmpList.get(i));
                    Bundle bundle1 = new Bundle();
                    allDatas.add(datas);
                    bundle1.putStringArrayList("datas",datas);
//                    fragments.add(X3AloneHotCommentFragment.getInstance(bundle1));
                    fragments.add(null);
                }
                if (tmpList.size()%2 == 1 && i == tmpList.size()-1){
                    ArrayList<String> datas = new ArrayList<String>();
                    datas.add(tmpList.get(tmpList.size()-1));
                    allDatas.add(datas);
                    Bundle bundle1 = new Bundle();
                    bundle1.putStringArrayList("datas",datas);
//                    fragments.add(X3AloneHotCommentFragment.getInstance(bundle1));
                    fragments.add(null);
                }
            }

        }

        ArrayList<View> views = new ArrayList<>();

        for (int i=0; i < fragments.size();i++ ){
            View v = LayoutInflater.from(activity).inflate(R.layout.x3_alone_hot_comment_fragment, null);
            views.add(v);
        }

        if (fragments.size() <=1){
            rl_indicator.setVisibility(View.INVISIBLE);
        }else{
            rl_indicator.setVisibility(View.VISIBLE);
        }

        vp_main.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View v = views.get(position);
                TextView tv_one = v.findViewById(R.id.tv_one);
                TextView tv_two= v.findViewById(R.id.tv_two);
                ArrayList<String> data = allDatas.get(position);
                if (!X3StringUtils.isListEmpty(data)){
                    tv_one.setText(data.get(0));
                    if (data.size() > 1){
                        tv_two.setText(data.get(1));
                        tv_two.setVisibility(View.VISIBLE);
                    }else{
                        tv_two.setVisibility(View.GONE);
                    }
                }
                tv_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onHotClick.onHotClick(tv_one.getText().toString().trim());
                    }
                });
                tv_two.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onHotClick.onHotClick(tv_two.getText().toString().trim());
                    }
                });
                container.addView(v);
                return v;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(views.get(position));
            }
        });

        vp_main.setCurrentItem(0);
        initTabLayout();
    }

    private void initTabLayout() {
        id_view.setSliderColor(Color.parseColor("#D8D8D8"), Color.parseColor("#ffffff"));
        id_view.setSliderWidth(X3ScreenUtils.dipToPx(6,activity), X3ScreenUtils.dipToPx(6,activity));
        id_view.setIndicatorGap(X3ScreenUtils.dipToPx(4,activity));
        id_view.setSliderHeight(X3ScreenUtils.dipToPx(6,activity));
        id_view.setSlideMode(IndicatorSlideMode.WORM);
        id_view.setIndicatorStyle(IndicatorStyle.CIRCLE);
        id_view.setupWithViewPager(vp_main);
    }
    OnHotClick onHotClick;
    public interface OnHotClick{
        void onHotClick(String content);
    }

}
